// pages/userCenter/merIdentify.js
var rsa = require('../../utils/rsa.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src:'',
    bsrc:'',
    csrc:'',
    dsrc:'',

    image_id1:'',
    image_id2:'',
    image_id3:'',
    image_id4:'',

    identify:'',   //身份证


    openbank:'',  
    cardnum:'',
    uname:'',
    tel:'',
    address:'',
    useflag:false,
    known:true,
    xiyi_text:'',
    region: ['--省', '--市', '--区'],
    parray:[],
    carray:[],
    pindex:0,
    cindex:0,
    isfirstload:true,
    showbank:false,
    banks:[],
    bank_code:'',
    city_code:'',//城市code
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
    this.showprovince();
  },
  sch:function(){
    this.setData({
      useflag:false,
    })
  },
  //使用协议
  showmodele:function(){
    // var xiy = this.data.xiyi_text;
    // if(xiy ==''){
    //   this.requireNode();
    // }
    // this.setData({
    //   useflag:true,
    // })
    this.requireNode();
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //请求协议
  requireNode:function(){
   wx.downloadFile({
     url: rsa.DOCHTTP,
      success: function (res) {
       // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
       if (res.statusCode === 200) {
         var filePath = res.tempFilePath
         wx.openDocument({
           filePath: filePath,
           success: function (res) {
             console.log('打开文档成功')
           }
         })
       }
     }

   })
    // rsa.httpPost('basic_agreement', {}, (err, res) => {
    //   if (err) {
    //     wx.showToast({
    //       title: err,
    //       icon: 'none',
    //       duration: 4000,
    //     })
    //     return;
    //   }
    //   this.setData({
    //     xiyi_text: res.data.message
    //   })
     
    // })
  },
  //checkbox
  checkboxChange:function(e){
    console.log(e);
  
    const vale = e.detail.value;
    var isknow = true;
    if(vale == '1'){
      isknow = true;
    }else{
      isknow = false;
    }
    this.data.known = isknow;
  
  },
  // 拍照
  getLocalImage: function (e) {
    var that = this;
    const merid = app.globalData.merId;
    const index = e.currentTarget.dataset.info;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        // 这里无论用户是从相册选择还是直接用相机拍摄，拍摄完成后的图片临时路径都会传递进来
        var filePath = res.tempFilePaths[0];
        // 这里顺道展示一下如何将上传上来的文件返回给后端，就是调用wx.uploadFile函数
        wx.uploadFile({
          url: rsa.PHOST,
          filePath: filePath,
          name: 'file',
          success: function (res) {
            var redata = JSON.parse(res.data);
            var recode = redata.return_code;
            if (recode != '0000') {
              wx.showToast({
                title: '上传失败',
                icon:'none',
                duration:3000,
              })
              return;
            }
            var imgcode = redata.data.image_id;
            if(index == '1'){
              that.setData({
                src:filePath,
                image_id1:imgcode,
              })
            }else if(index == '2'){
              that.setData({
                image_id3:imgcode,
                bsrc:filePath,
              })

            }else if(index =='3'){
              that.setData({
                image_id2:imgcode,
                csrc:filePath,
              })
            }else{
              that.setData({
                image_id4:imgcode,
                dsrc:filePath,
              })
            }

          } 
        })
      },
      fail: function (error) {
        console.error("调用本地相册文件时出错")
      },
      complete: function () {

      }
    })
  },
  //提交认证
  uploadAction:function(){
    const isknow = this.data.known;
    if(!isknow)return;
    var mess = '';
    const bcode = this.data.bank_code;   //结算银行
    const carnum = this.data.cardnum;
    const name = this.data.uname;
    const idy = this.data.identify;  //身份证

    const cityCode = this.data.city_code;  //城市编号

    const address = this.data.address;   //详细地址
    const val1 = this.data.image_id1;
    const val2 = this.data.image_id2;
    const val3 = this.data.image_id3;
    const val4 = this.data.image_id4;

  
    if (!bcode){
      mess = "开户行不为空";
    }else if(!carnum){
      mess = "卡号不为空";
    }else if(!name){
      mess = "持卡人姓名必须填写";
    } else if (!idy){
      mess = "身份证号必填";
    } else if (!val1){
      mess = '身份证正面必须上传';
    } else if (!val2){
      mess = '身份证反面必须上传';
    }else if(!val3){
      mess = '银行卡正面必须上传';
    }else if(!val4){
      mess = '银行卡反面必须上传';
    }
    if(mess){
      wx.showToast({
        title: mess,
        icon:'none',
        duration:3000,
      })
      return;
    }
    const merid = app.globalData.merId;
    const session = app.globalData.session;
    rsa.htp('promotion.addstore', { "seller_name": name, 'id_card': idy, 'bank_name': bcode, "bank_card": carnum, 'area': cityCode, 'addr': address, 'image_id1': val1, "image_id2": val2, "image_id3": val3, "image_id4": val4,'session_id':session }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 4000,
        })
        return;
      }
      if (res.return_code == '0000'){
        wx.showModal({
          title: '提示',
          content: '提交成功，等待审核',
          cancelText: '返回',
          success: function (res) {
            if (res.confirm) {
            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '提交失败',
          icon:'none',
          duration:3000,
        })
      }
    })
  },
//查询省
  showprovince:function(){
    rsa.htp('promotion.getpro', {}, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 4000,
        })
        return;
      }
      const isf = this.data.isfirstload;
      this.setData({
        parray: res.data.list,
      })
      if(isf){
        this.showcity();
      }
      this.setData({
        isfirstload:false
      })
    })
  },
  bindPickerChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      pindex:e.detail.value
    })
    this.showcity();
  },
  //查询市
  showcity:function(){
    const index = this.data.pindex;
    const parr = this.data.parray;
    if(parr.length==0) return;
    var prname = parr[index].pro;
    rsa.htp('promotion.getcity', {'pro':prname}, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 4000,
        })
        return;
      }
      var code= '';
      var lst = res.data.list;
      if (lst.length>0){
        code = lst[0].region_code;
      }
      this.setData({
        carray: lst,
        city_code:code,
      })
    })
  },

  bindPickerChange2:function(e){
    const citys = this.data.carray;
    const index = e.detail.value;
    var grp = citys[index];
    this.setData({
      cindex:e.detail.value,
      city_code: grp.region_code,
    })
  },
  openbank:function(e){
    this.setData({
      openbank: e.detail.value
    })
  },
  cardNumA:function(e){
    this.setData({
      cardnum:e.detail.value,
    })
  },
  identfyAciton:function(e){
    this.setData({
      identify:e.detail.value,
    })
  },
  userNameA:function(e){
    this.setData({
      uname:e.detail.value,
    })
  },
  telephoneA:function(e){
    this.data.tel = e.detail.value;
  },
  addressA:function(e){
    this.setData({
      address: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  canclepickview:function(){
    this.setData({
      showbank:false
    })
  },
  //查询银行
  checkbank:function(){
    const opbank = this.data.openbank;
    if (opbank == '' || !opbank) return;
    rsa.htp('promotion.banksearch', { 'bank_name': opbank }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 4000,
        })
        return;
      }
      this.setData({
        banks:res.data.list,
         showbank: true,
      })
    })
  },
  //选择绑定的银行
  bindBankChange:function(e){
    const val = e.detail.value;
    const bk = this.data.banks;
    var grop =   bk[val[0]];
    var bk_code = grop.id;
    this.setData({
      bank_code:bk_code,
      openbank: grop.bank_name,
    })
  },

})